package thread;

public class SyncDemo2 {
    public static void main(String[] args) {
//        Shop shop=new Shop();
        Shop shop1=new Shop();
        Shop shop2=new Shop();
//        Thread t1=new Thread("范传奇"){
//            public void run(){
//                shop1.buy();
////                shop.buy();
//            }
//
//        };
//        Thread t2=new Thread("王克晶"){
//            public void run(){
//                shop2.buy();
////                shop.buy();
//            }
//        };
//        Runnable r1=new Runnable() {
//            @Override
//            public void run() {
//                shop1.buy();
//            }
//        };
//        Thread t1=new Thread(r1);
        //第二种
//        Runnable r1=()->shop1.buy();
//        Thread t1=new Thread(r1);
        //第三种
//        Thread t1=new Thread(()->shop1.buy());
        Thread t1=new Thread(shop1::buy);
        Thread t2=new Thread(shop2::buy);
        t1.start();
        t2.start();
    }
}
class Shop{
   public  void buy(){
        try {
            Thread t=Thread.currentThread();
            System.out.println(t.getName()+"正在挑衣服...");
            Thread.sleep(3000);
            synchronized(this) {//()枷锁的对象,同步监视器的对象
                System.out.println(t.getName() + "正在试衣服...");
                Thread.sleep(3000);
            }
            System.out.println(t.getName()+"正在结账...");
        } catch (InterruptedException e) {
        }

    }
}
